<?php

/**
 * Salarie form.
 *
 * @package    sopN
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfPropelFormTemplate.php 10377 2008-07-21 07:10:32Z dwhittle $
 */
class SalarieForm extends BaseSalarieForm {

  public function configure() {
    // On récupère les utilisateurs de l'application
    $c = new Criteria();
    $c->addAscendingOrderByColumn(sfGuardUserPeer::USERNAME);
    $this->users = sfGuardUserPeer::doSelect($c);

    $tabUsers = array(0 => '');
    foreach ($this->users as $user)
      $tabUsers[$user->getId()] = $user->getUsername();

    // Modification du champ "guard_id"
    $utilsWidget = new sfWidgetFormChoice(array(
        'choices' => $tabUsers,
        'label' => 'Utilisateur associé'
            ));

    $this->widgetSchema['guard_id'] = $utilsWidget;
    $this->setDefault('guard_id', $this->getObject()->getActif());

    $this->validatorSchema['guard_id'] = new sfValidatorInteger();

    // Modification du champ "actif"
    $actifWidget = new sfWidgetFormChoice(array('choices' =>
        array(1 => 'Oui', 0 => 'Non'), 'label' => 'Actif'));

    $this->widgetSchema['actif'] = $actifWidget;
    $this->setDefault('actif', $this->getObject()->getActif());

    $this->validatorSchema['actif'] = new sfValidatorBoolean();
    
    $this->widgetSchema['asso_salarie_unite_fonctionnelle_list']->addOption('renderer_class', 'sfWidgetFormSelectDoubleListSop')
                                                                ->addOption('renderer_options', array(
                                                                    'associated_first'    => false, 
                                                                    'label_unassociated'  => "Non associ&eacute;", 
                                                                    'label_associated'    => "Associ&eacute;"
                                                                ))
                                                                ->addOption('label', "Unités associés")
                                                                ->setAttribute('size', 5);
    $this->validatorSchema['asso_salarie_unite_fonctionnelle_list'] = new sfValidatorPass(array('required' => false));
  }

  public function saveAssoSalarieUniteFonctionnelleList($con = null) {
    if (!$this->isValid())
      throw $this->getErrorSchema();

    if (!isset($this->widgetSchema['asso_salarie_unite_fonctionnelle_list']))
      return;

    if (null === $con)
      $con = $this->getConnection();
    
    $c = new Criteria();
    $c->add(AssoSalarieUniteFonctionnellePeer::SALARIE_ID, $this->object->getPrimaryKey());
    AssoSalarieUniteFonctionnellePeer::doDelete($c, $con);

    $values = $this->getValue('asso_salarie_unite_fonctionnelle_list');
    if (is_array($values)) {
      foreach ($values as $value) {
        $obj = new AssoSalarieUniteFonctionnelle();
        $obj->setSalarieId($this->object->getPrimaryKey());
        $obj->setUfId($value);
        $obj->save();
      }
    }
  }

}
